import { Component, OnInit, Input, OnChanges } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
import { Router } from '@angular/router';
import { HttpServcie } from '../../../../service/http.service';
import { Config } from '../../../../service/config';
import { TractionHalfMonBean } from './bean/tractionHalfMon.bean';
import { el } from "@angular/platform-browser/testing/src/browser_util";
import { NzModalService, NzModalSubject } from 'ng-zorro-antd';
import { CommFuntion } from '../../../../service/commFunction';
@Component({
  selector: 'traction-list',
  templateUrl: './traction.html',
  styleUrls: ['./traction.scss']
})
/**
 * 曳引电梯列表
 */
export class TractionPage implements OnInit, OnChanges {
  //获取电梯的id
  @Input() inValue: string;

  data: any;
  list: any = [];//返回的数据
  tractionHalfMonBean: TractionHalfMonBean = new TractionHalfMonBean();
  types: any = [
    { typeName: '半月保', value: '1' },
    { typeName: '季度保', value: '2' },
    { typeName: '半年保', value: '3' },
    { typeName: '年保', value: '4' }
  ]; //电梯类型
  maintTypes = Config.MAINTTYPES;
  config: any;
  modelTitle: string;//模态标题
  currentModal: NzModalSubject;
  isActive: number = 1;
  total: string;
  type: number; //判断是什么类型的维保
  tractionHalfMon: any; //半月保
  tractionQuarter: any;//季度保
  tractionHalfYear: any;//半年保
  tractionYear: any;//年保
  totalImg: any;
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService,
    private commFuntion: CommFuntion
  ) {
    this.config = Config;
  }

  ngOnInit() {

  }
  ngOnChanges() {
    this.tractionHalfMonBean.pageSize = 10;
    this.tractionHalfMonBean.currentPage = 1;
    this.tractionHalfMonBean.elevatorNum = this.inValue;
    this.query();
  }


  query() {
    this.httpServcie.queryTraction(this.tractionHalfMonBean).subscribe(datas => {
      if (datas.status == 200) {
        this.list = datas.data.list;
        this.total = datas.data.total;
      }

    })
  }

  refreshStatus() {

  }
  displayDataChange() {

  }
  //点击头tab 切换
  doActive(ind) {
    this.isActive = ind;
  }
  toDetail(titleTpl, contentTpl, footerTpl, data) {
    this.modelTitle = '维保详细';
    this.type = data.type;
    //查询维保的信息
    this.httpServcie.queryTractionDetails(data.id).subscribe(datas => {
      if (datas.status == 200) {
        this.tractionHalfMon = datas.data.traction.tractionHalfMon;
        this.tractionQuarter = datas.data.traction.tractionQuarter;//季度保
        this.tractionHalfYear = datas.data.traction.tractionHalfYear;//半年保
        this.tractionYear = datas.data.traction.tractionYear;//年保
      }
      let num = 0;
      let tractionHalfMon = this.commFuntion.convertBean(this.tractionHalfMon);
      num += this.commFuntion.countPicNum(tractionHalfMon);
      if (this.tractionQuarter) {
        let tractionQuarter = this.commFuntion.convertBean(this.tractionQuarter);
        num += this.commFuntion.countPicNum(tractionQuarter);
      }
      if (this.tractionHalfYear) {
        let tractionHalfYear = this.commFuntion.convertBean(this.tractionHalfYear);
        num += this.commFuntion.countPicNum(tractionHalfYear);
      }
      if (this.tractionYear) {
        let tractionYear = this.commFuntion.convertBean(this.tractionYear);
        num += this.commFuntion.countPicNum(tractionYear);
      }
      this.totalImg = num;
    })
    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 800,
      style: {
        top: '20px'
      },
      onOk() {
        console.log('Click ok');
      }
    });
  }

  //分页
  nzPageSizeChange(event) {
    this.tractionHalfMonBean.pageSize = event;
    this.tractionHalfMonBean.currentPage = 1;
    this.query();

  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.tractionHalfMonBean.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.tractionHalfMonBean.currentPage = event;
    this.query();
  }

  export(data) {
    let param = { halfMonId: data.id, elevatorNum: data.elevatorNum, elevatorType: '1' };
    this.httpServcie.exportMaint(param).subscribe(data => {
      if (data.status == 200) {
        this.download(data.data.fileUrl, data.data.fileName);
        this._notification.create('success', '提示', "导出成功");
      } else {
        this._notification.create('error', '提示', "导出失败");
      }
    })
  }
  download(url, name) {
    let link = document.createElement('a');
    let body = document.querySelector('body');

    this.getBlob(this.config.IP + this.config.API.getUpload + url).then(blob => {
      link.href = window.URL.createObjectURL(blob);
      link.download = name;
      // fix Firefox
      link.style.display = 'none';
      body.appendChild(link);
      link.click();
      body.removeChild(link);
      window.URL.revokeObjectURL(link.href);
    });
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open('GET', url, true);
      xhr.responseType = 'blob';
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }
}
